import api from './api.js' 
import {appid,appsecret} from '../config.js'
 
// 登录状态
const loginState = async function(type){	
	let token_cache = uni.getStorageSync('token');
	let isLogin='';
	
	if(type&&type=='reset'){
		uni.showToast({
			icon:'loading',
			title:'请先登录...',
			mask:true,
			duration:1000
		})
		setTimeout(()=>{
			uni.navigateTo({
				url:'/pages/login/login?backType=prev'
			})
		},1000)
	}else if(type&&type=='check'){
		if(!token_cache){
			uni.showToast({
				icon:'loading',
				title:'请先登录...',
				mask:true,
				duration:1000
			})
			isLogin = false;
		}else{
			let res=await api.user_api.getToken({
				appid:appid,
				appsecret:appsecret
			}) 
			if(res.data.ret==1000){
				let token=res.data.data.token;
				if(token_cache==token){
					isLogin = true; 
				}else{				
					uni.showToast({
						icon:'loading',
						title:'登录已超时...',
						mask:true,
						duration:1000
					})
					uni.removeStorageSync('userInfo');
					uni.removeStorageSync('token');
					uni.removeStorageSync('cookieKey');
					
					isLogin = false; 
				}			 
			}
		}  
	}  
	
	
		
	return isLogin
}

const loginState1 = async function(type){	
	let token_cache = uni.getStorageSync('token');  
	let isLogin='';
	
	
	
	if(!token_cache){
		uni.showToast({
			icon:'loading',
			title:'请先登录...',
			mask:true,
			duration:1000
		})
		isLogin = false;
	}else{
		let res=await api.user_api.getToken({
			appid:appid,
			appsecret:appsecret
		})
		console.log(res)
		if(res.data.ret==1000){
			let token=res.data.data.token;
			if(token_cache==token){
				isLogin = true; 
			}else{				
				uni.showToast({
					icon:'loading',
					title:'登录已超时...',
					mask:true,
					duration:1000
				})
				uni.removeStorageSync('userInfo');
				uni.removeStorageSync('token');
				uni.removeStorageSync('cookieKey');
				
				isLogin = false; 
			}			 
		}
	}
	
	if(type&&type=='reset'&&!isLogin){
		setTimeout(()=>{
			uni.navigateTo({
				url:'/pages/login/login?backType=prev'
			})
		},1000)
	}  
		
	return isLogin
}

const loginReset = () =>{
	uni.navigateTo({
		url:'/pages/login/login?backType=prev'
	})
}


export { 
	loginState
	
}